from flask import Flask,request
from flask import render_template
# 调用自定义模块中的函数
from reserch import reserch4letters
# from wsgiref.simple_server import make_server

# Flask： 中央登记中心
app = Flask(__name__)

# 【Flask】(https://dormousehole.readthedocs.io/en/latest/quickstart.html#id10)
# 【HTMl】(https://www.w3school.com.cn/html/index.asp)
# 【jinja2】(https://palletsprojects.com/p/jinja/)

# 1. 路由：url
# 1.1 相对路径（一部分，不是完整），一部分：相对 IP地址的，endpoint （终结点）
# 1.2 request, methods [GET默认] [POST]
# 2. 视图函数:
# 2.1 代码块，用来处理数据的 (python核心).
# 2.2 返回值：'Hello World!' 文本（用户体验极低，且不容易处理）
#            html文件， (用户体验高，UI美观简洁）
#            Json文件， 数据处理方便，常用于数据传递 (期末不用）


# 1. 首页
@app.route('/',methods=['GET'])
def hello_world():
    return render_template('hello.html')

# 2. 名字应用
@app.route('/index',methods=['GET','POST'])
def index():
    return render_template('index.html')

#  元音字母
@app.route('/index1',methods=['GET','POST'])
def index1():
    return render_template('index1.html')

# 名字应用结果页
@app.route('/result',methods=['GET','POST'])
def result():
    #用户数据提交到这里了
    py_firstname = request.form['firstname']
    py_lastname = request.form['lastname']
    py_fullname = py_firstname.title() + '' + py_lastname.title()

    return render_template('result.html',
                           fullname = py_fullname,)

# 元音字母结果页
@app.route('/vowels',methods=['GET','POST'])
def vowels():
    py_word = request.form['word']
    py_vowels = request.form['vowels']
    py_found = reserch4letters(py_vowels, py_word)
    return render_template('vowels.html',
                           found=py_found)

# 购物车
@app.route('/shop',methods=['GET','POST'])  #登录页
def shop():
    return render_template('shop.html')

@app.route('/userinfo',methods=['GET','POST']) #显示页
def userinfo():
    py_email = request.form.get('inputEmail')
    py_password = request.form.get('inputPassword')
    py_result = py_email.title() + '登陆成功' + py_password.title()
    return render_template('userinfo.html',
                           result = py_result)

@app.route('/cart',methods=['GET','POST']) #购物车页面
def cart():
    return render_template('cart.html')

# 随机背景颜色
@app.route('/background', methods=['GET'])
def background():
    return render_template('background.html')



if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000,debug=True)


# @app.route('/index', methods=['POST'])
# def do_search() -> 'html':
#     phrase = request.form['phrase']
#     letters = request.form['letters']
#     title = 'Here are your results:'
#     results = str(reserch4letters(phrase, letters))
#     return render_template('index.html',
#                            the_title=title,
#                            the_phrase=phrase,
#                            the_letters=letters,
#                            the_results=results,)
# @app.route('/')
# @app.route('/index')
# def entry_page() -> 'html':
#     return render_template('index.html',
#                            the_title='Welcome to search4letters on the web!')

# @app.route('/vowels')
# def view_the_log() -> 'html':
#     contents = []
#     with open('vsearch.log') as log:
#         for line in log:
#             contents.append([])
#             for item in line.split('|'):
#                 contents[-1].append(escape(item))
#     titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results')
#     return render_template('vowels.html',
#                            the_title='View Log',
#                            the_row_titles=titles,
#                            the_data=contents,)

